package com.demo2do.fms.admin.service.supplier.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.SupplierEventDao;
import com.demo2do.fms.admin.entity.search.supplier.SupplierEventSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.SupplierEventService;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierEvent;

/**
 *
 * @author Ark
 *
 */
@Service("supplierEventService")
public class SupplierEventServiceImpl extends DataServiceImpl<SupplierEvent> implements SupplierEventService {

    @Dao
    private SupplierEventDao supplierEventDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#count(com.demo2do.fms.admin.entity.security.Login)
     */
    public int count(Login login) {
        
        if (login.isSupplier()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            return supplierEventDao.count(supplier);
            
        } else {
            return supplierEventDao.count();
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#list(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<SupplierEvent> list(Login login, Page page) {
        
        page.calculate();
        
        if (login.isSupplier()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            return supplierEventDao.list(supplier, page.getBeginIndex(), page.getEveryPage());
            
        } else {
            return supplierEventDao.list(page.getBeginIndex(), page.getEveryPage());
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#count(com.demo2do.fms.admin.entity.search.supplier.SupplierEventSearch)
     */
    public int count(SupplierEventSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#list(com.demo2do.fms.admin.entity.search.supplier.SupplierEventSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<SupplierEvent> list(SupplierEventSearch search, Page page) {
        
        // 1. build sentence
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 2. do query
        List<SupplierEvent> events = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return events;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#count(java.util.List)
     */
    public int count(List<Long> supplierIds) {
        if (CollectionUtils.isEmpty(supplierIds)) {
            return 0;
        }
        return supplierEventDao.count(supplierIds);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.SupplierEventService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<SupplierEvent> list(List<Long> supplierIds, Page page) {
        List<SupplierEvent> result = supplierEventDao.list(supplierIds, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }
    
}
